package fr.lox.fall.common;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class Map {
	
	private Texture _background, _bgRep, _wall, _wallRep1;
	private Vector2 posW, posBG;
	private Player p;
	private float w = 0f, h = 0f, _speed = 8f, x1 = 0f, y1 = 0f - (Gdx.graphics.getHeight() - 15f), y2 = -((Gdx.graphics.getHeight() * 2) - 15f);
	private float y3 = y1, y4 = y2;
	private boolean scroll = false;

	
	public Map()
	{
		this._background = new Texture(Gdx.files.internal("images/map/background.png"));
		this._wall = new Texture(Gdx.files.internal("images/map/wall.png"));
		this._bgRep = new Texture(Gdx.files.internal("images/map/bgRep.png"));
		this._wallRep1 = new Texture(Gdx.files.internal("images/map/wallRep.png"));
		this.posW = new Vector2(0f, 0f);
		this.posBG = new Vector2(0f, 0f);
	}
	
	private void repeat(SpriteBatch batch)
	{
		int nb = 0;
		int i = 0;
		
		if(y1 >= h)
			y1 = -( h - 15f);
		if(y2 >= h)
			y2 = -(h - 15f);
		
		if(y3 >= h)
			y3 = -( h - 15f);
		if(y4 >= h)
			y4 = -(h - 15f);

		batch.draw(this._bgRep, 0, y3, w, h);
		batch.draw(this._bgRep, 0, y4, w, h);
		
			batch.draw(this._wallRep1, 0, y1, w, h);
			batch.draw(this._wallRep1, 0, y2, w, h);

				
		this.y1 += _speed;
		this.y2 += _speed;
		
		this.y3 += 5f;
		this.y4 += 5f;
		
		/*
		System.out.println("y1 - " + y1);
		System.out.println("y2 - " + y2);*/

		//nb = 0;
	}
	
	public void scrolling()
	{
		this.posW.y += _speed;
		this.posBG.y += 5f;
	}
	
	public void update(Player p)
	{
		if (p.getIsRun() && (p.getY() <= Gdx.graphics.getHeight() / 2))
		{
			this.scrolling();
			this.scroll = true;
			p.incrementScore(10);
		}
		else
		{
			scroll = false;
		}
	}
	
	public void resize(int width, int height)
	{
		this.w = width;
		this.h = height;
	}
	
	public void draw(SpriteBatch batch)
	{
		batch.draw(_background, this.posBG.x, this.posBG.y, w, h);
		batch.draw(_wall, this.posW.x, this.posW.y, w, h);
		if (scroll)
			this.repeat(batch);
	}

}
